package com.rudolfheszele.smsselector.model;

public class LogicalOperationFactory
{
	public enum OperationId implements Id
	{
		AND("AND", 0),
		OR("OR", 1),
		XOR("XOR", 2),
		NOT("NOT", 3);
		
		@Override
		public int getIntValue()
		{
			return mIntValue;
		}
		
		@Override
		public String getName()
		{
			return mRuleName;
		}
		
		private OperationId(String ruleName, int intValue)
		{
			mRuleName = ruleName;
			mIntValue = intValue;
		}
		
		private int mIntValue;
		private String mRuleName;
	}
	
	public static SmsSelectorRuleSet.LogicalOperation createOperation(OperationId operationId)
	{
		SmsSelectorRuleSet.LogicalOperation operation = null;
		
		switch(operationId)
		{
			case AND:
				operation = new AndLogicalOperation();
				break;
			case OR:
				operation = new OrLogicalOperation();
				break;
			case XOR:
				operation = new XorLogicalOperation();
				break;
			case NOT:
				operation = new NotLogicalOperation();
				break;
		}
		if(operation != null)
			operation.setId(operationId);
		
		return operation;
	}
	
	public static OperationId getOperationId(int operationId)
	{
		OperationId[] operationIds = OperationId.values();
		
		for(int i = 0; i < operationIds.length; i++)
		{
			if(operationId == operationIds[i].getIntValue())
				return operationIds[i];
		}
		
		return null;
	}
	
	public static OperationId getOperationId(String operationName)
	{
		OperationId[] operationIds = OperationId.values();
		
		for(int i = 0; i < operationIds.length; i++)
		{
			if(operationName.equals(operationIds[i].getName()))
				return operationIds[i];
		}
		
		return null;
	}
	
	public static int getNumberOfOperations()
	{
		return OperationId.values().length;
	}
}

class AndLogicalOperation implements SmsSelectorRuleSet.LogicalOperation
{
	@Override
	public boolean doOperation(boolean valueA, boolean valueB)
	{
		return valueA && valueB;
	}

	@Override
	public Id getId()
	{
		return mId;
	}
	
	@Override
	public void setId(Id id)
	{
		mId = id;
	}
	
	private Id mId;
}

class OrLogicalOperation implements SmsSelectorRuleSet.LogicalOperation
{
	@Override
	public boolean doOperation(boolean valueA, boolean valueB)
	{
		return valueA || valueB;
	}

	@Override
	public Id getId()
	{
		return mId;
	}
	
	@Override
	public void setId(Id id)
	{
		mId = id;
	}
	
	private Id mId;
}

class XorLogicalOperation implements SmsSelectorRuleSet.LogicalOperation
{
	@Override
	public boolean doOperation(boolean valueA, boolean valueB)
	{
		return valueA ^ valueB;
	}

	@Override
	public Id getId()
	{
		return mId;
	}
	
	@Override
	public void setId(Id id)
	{
		mId = id;
	}
	
	private Id mId;
}

class NotLogicalOperation implements SmsSelectorRuleSet.LogicalOperation
{

	@Override
	public boolean doOperation(boolean valueA, boolean valueB)
	{
		// valueB is ignored here
		return !valueA;
	}

	@Override
	public Id getId()
	{
		return mId;
	}
	
	@Override
	public void setId(Id id)
	{
		mId = id;
	}
	
	private Id mId;
}